package cn.bigway.app.weavingdesigner.util;

import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Vector;

import javax.swing.JButton;
import javax.swing.JPanel;

import cn.bigway.app.weavingdesigner.swing.WdDialog;

abstract public class WdStepDialog extends WdDialog implements ActionListener {

	private static final long serialVersionUID = 4254486682635041331L;

	private Vector<WdStepPanel> panels;

	private JPanel cardPanel;

	private int cardIndex;

	private JButton backButton, nextButton, finishButton, cancelButton;

	public WdStepDialog() {
		this.cardIndex = -1;
		this.panels = new Vector<WdStepPanel>();
		this.cardPanel = new JPanel(new CardLayout());

		this.backButton = new JButton("back");
		this.nextButton = new JButton("next");
		this.finishButton = new JButton("finish");
		this.cancelButton = new JButton("cancel");

		this.backButton.addActionListener(this);
		this.nextButton.addActionListener(this);
		this.finishButton.addActionListener(this);
		this.cancelButton.addActionListener(this);

		this.backButton.setVisible(false);
		this.nextButton.setVisible(false);
		this.finishButton.setVisible(false);

		JPanel btnPanel = new JPanel(new FlowLayout());
		btnPanel.add(this.backButton);
		btnPanel.add(this.nextButton);
		btnPanel.add(this.finishButton);
		btnPanel.add(this.cancelButton);

		getContentPane().add(this.cardPanel, BorderLayout.CENTER);
		getContentPane().add(btnPanel, BorderLayout.SOUTH);
	}

	@Override
	final public void showDialog() {
		showStepPanel(0);
		super.showDialog();
	}

	@Override
	final public void actionPerformed(ActionEvent event) {
		if (event.getSource().equals(this.backButton)) {
			showStepPanel(this.cardIndex - 1);
		} else if (event.getSource().equals(this.nextButton)) {
			showStepPanel(this.cardIndex + 1);
		} else if (event.getSource().equals(this.finishButton)) {

		} else if (event.getSource().equals(this.cancelButton)) {
			dispose();
		}
	}

	protected void addStepPanel(WdStepPanel panel) {
		this.cardPanel.add(panel, "" + this.panels.size());
		this.panels.add(panel);
	}

	private void showStepPanel(int index) {
		if (index == this.cardIndex || index < 0 || index >= this.panels.size()) {
			return;
		}

		this.backButton.setEnabled(false);
		this.nextButton.setEnabled(false);
		this.finishButton.setEnabled(false);

		if (index > this.cardIndex) {
			if (this.cardIndex < 0
					|| this.panels.get(this.cardIndex).verify(this)) {
				this.cardIndex = index;
				this.panels.get(this.cardIndex).beforeEnter(this);
			}
		} else {
			this.cardIndex = index;
		}

		((CardLayout) this.cardPanel.getLayout()).show(this.cardPanel, ""
				+ this.cardIndex);

		this.backButton.setVisible(this.cardIndex > 0);
		this.nextButton.setVisible((this.cardIndex + 1) < this.panels.size());
		this.finishButton
				.setVisible((this.cardIndex + 1) == this.panels.size());

		this.backButton.setEnabled(true);
		this.nextButton.setEnabled(true);
		this.finishButton.setEnabled(true);
	}
}
